package com.xiaoyi.query;

import com.xiaoyi.equery.EQueryModel;
import com.xiaoyi.pooled.PooledDataSource;
import com.xiaoyi.unpooled.UnpooledDataSource;

import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;

/**
 * @author xiaoyi
 * @version 1.0.0
 * @createTime 2023-02-21 22:50:00
 */
public class MysqlQuery extends AbstartQuery {

    @Override
    public PooledDataSource getPooledDataSource(EQueryModel eQueryModel) throws MalformedURLException {

        UnpooledDataSource dataSource = null;
        if (2 == eQueryModel.getDriverFrom()) {
            URLClassLoader driverClassLoader = new URLClassLoader(new URL[]{new URL(eQueryModel.getDriverUrl())});
            dataSource = new UnpooledDataSource(driverClassLoader, eQueryModel.getDriverClass(), eQueryModel.getUrl(), eQueryModel.getUsername(), eQueryModel.getPassword());
        } else {
            dataSource = new UnpooledDataSource(eQueryModel.getDriverClass(), eQueryModel.getUrl(), eQueryModel.getUsername(), eQueryModel.getPassword());
        }
        PooledDataSource pooledDataSource = new PooledDataSource(dataSource);
        return pooledDataSource;
    }

    @Override
    public String getCountName() {
        return "count";
    }
}
